<?php

include('../DB.php');

// 爬取任务处理

class Worker
{
    private $options;
    private $queueDB;
    private $writeDB;
    private $readDB;
    private $info;
    private $worker;
    private $gearmanConfig;

    public function __construct($argv)
    {
        $dbConfig = include('../dbconfig.php');
        $gearmanConfig = include('../gearmanconfig.php');

        $this->gearmanConfig = $gearmanConfig;

        $this->parseArgv($argv);
        $this->connDB($dbConfig);
        $this->getGearmanWorker($gearmanConfig);
    }

    public function parseArgv($argv)
    {
        $shortOpts = "v::h::l:";
        $longOpts = ["verbose::", "help::", "limit:"];
        $options = getopt($shortOpts, $longOpts);

        foreach ($options as $k => $v) {
            if ($k=='h' || $k=='help') {
                $this->help();
            }
            if ($k=='l' || $k=='limit') {
                $this->options['limit'] = $v;
            }
            if ($k== 'v' || $k== 'verbose') {
                $this->info = true;
            }
        }
    }

    public function connDB($dbConfig)
    {
        $config = $dbConfig['queue'];
        $this->queueDB = new DB($config['host'], $config['user'], $config['pass'], $config['dbname']);

        $config = $dbConfig['write'];
        $this->writeDB = new DB($config['host'], $config['user'], $config['pass'], $config['dbname']);

        $config = $dbConfig['read'];
        $this->readDB = new DB($config['host'], $config['user'], $config['pass'], $config['dbname']);
    }

    public function help()
    {
        echo 'help info...';
    }

    public function getGearmanWorker($gearmanConfig)
    {
        $worker= new GearmanWorker();
        $worker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
        $this->worker = $worker->addServer($gearmanConfig['ip'], $gearmanConfig['port']);
    }

    public function run()
    {
        $this->worker->addFunction("crawl_page", 'crawl_page');

        $time = time();
        $server = '1';

        while ( (time() - $time < 1000) && (
                    $this->worker->work()
                    || (GEARMAN_IO_WAIT == $this->worker->returnCode())
                    || (GEARMAN_NO_JOBS == $this->worker->returnCode())
                )
        ) {
            if (GEARMAN_SUCCESS == $this->worker->returnCode()) {
                continue;
            }

            $this->worker->wait();
        }

        $this->worker->unregisterAll();

        fwrite(STDOUT, sprintf('[%s] EXIT %s', date('H:i:s'), 'server '.$server) . "\n");
    }
}

function crawl_page($job)
{
    $workload = unserialize($job->workload());

    // 抓取任务，入库
    $url = $workload['url'];
    $contents = file_get_contents($url);
    if ($this->info) {
        echo $contents;
    }
    // 抓取发生错误
    $error = false;
    if ($error) {
        if ($workload['retry'] > 0) {   // 如果允许重试,则将重试次数减一，如果次数小于0，删除该任务
        } else { // 直接删除该条任务
        }
    } else {
        // 没有错误，则进行入库逻辑处理，删除该行队列表数据
    }
}

$worker = new Worker($argv);
$worker->run();
